﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraPrinting;
using MINERP.MODEL.FoundationModel;
using MINERP.COM;

namespace MINERP.Statistics.ShopBill
{
    public partial class FrmBillProduct : Form
    {
        private BLL.Statistics.ShopBillIndexBLL bll = new MINERP.BLL.Statistics.ShopBillIndexBLL();
        DataSet ds = null;
        private static FrmBillProduct _instance;
        private Shop_Info currentShop =null;

        public static  FrmBillProduct Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new FrmBillProduct();
                }
                else if (_instance.IsDisposed)
                {
                    _instance = new FrmBillProduct();
                }
                return _instance;

            }
        }
        private  FrmBillProduct()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            currentShop = (Shop_Info)cbShop.SelectedItem;
            if (currentShop == null)
            {
                MessageBox.Show("请选择商户！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
                
            try
            {
                btnOK.Enabled = false;
                FindBill();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            finally
            {
                btnOK.Enabled = true;
            }
        }


        private void FindBill()
        {
            try
            {
                DateTime start = dtStartTime.DateTime.Date;
                DateTime end = dtEndTime.DateTime.AddDays(1).Date.AddSeconds(-1);
                if (start >= end.AddDays(1))
                {
                    MessageBox.Show("时间范围不合要求！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                
                ds = bll.GetBillProduct(start, end,currentShop.ShopId);
                if (ds != null)
                {
                    gridControlForExport.DataSource = ds.Tables[0];
                    gridControlForExport.RefreshDataSource();

                }
                else
                {
                    MessageBox.Show("没有符合条件的数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            try
            {
                if (ds == null || ds.Tables.Count == 0)
                {
                    MessageBox.Show("没有数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                else
                {
                    ds.Tables[0].DefaultView.Sort="账单号 desc";
                    DataTable tableR= ds.Tables[0];
                    gridControlForExport.DataSource = tableR;



                    DevGridViewExport.ExportToExcel(currentShop.ShopName + " 产品汇总表", tableR);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void FrmBillProduct_FormClosing(object sender, FormClosingEventArgs e)
        {
            _instance.Dispose();
            _instance = null;
        }

        //private void textEditHelpCode_KeyDown(object sender, KeyEventArgs e)
        //{
        //    if (Keys.Enter == e.KeyCode)
        //    {
        //        cbShop.DisplayMember = "ShopName";
        //        cbShop.ValueMember = "ShopId";
        //        cbShop.DataSource = bll.GetShopInfoByHelpCode(textEditHelpCode.Text.Trim());


        //    }
        //}

        private void textEditHelpCode_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            List<Shop_Info> listShop = null;
            listShop = bll.GetShopInfoByHelpCode(e.NewValue.ToString());
            cbShop.DisplayMember = "ShopName";
            cbShop.ValueMember = "ShopId";
            cbShop.DataSource = listShop;
        }

        private void FrmBillProduct_Load(object sender, EventArgs e)
        {
            dtStartTime.Text = DateTime.Now.AddMonths(-1).Year + "-" + DateTime.Now.AddMonths(-1).Month + "-01";
            dtEndTime.Text = DateTime.Now.Year + "-" + DateTime.Now.Month + "-01";
        }
    }
}
